<!DOCTYPE html>
<html>
<head>
    <title>挂号--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>
    <!-- 引入身份证验证 js 库 -->
    <script type="text/javascript" src="../../Js/cusz/IdCard-Validate.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">

        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });

            loadReg();

            idCarCheck();
        })

        String.prototype.GetValue = function (para) {
            let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
            let r = this.substr(this.indexOf("\?") + 1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        // 获取挂号信息
        function loadReg() {
            let url = document.location.toString();
            let hosrId = url.GetValue("hosrId");
            $.ajax({
                url: `/registration/${hosrId}`,
                type: "GET",
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        console.log(res)
                        let reg = res.obj;
                        $("input[name='hosrName']").val(reg.hosrName);
                        $("input[name='hosrIdcar']").val(reg.hosrIdcar);
                        $("input[name='hosrMedical']").val(reg.hosrMedical);
                        $("input[name='hosrRegprice']").val(reg.hosrRegprice);
                        $("input[name='hosrPhone']").val(reg.hosrPhone);

                        $(`input[name='hosrSelfprice'][value='${reg.hosrSelfprice}']`).prop("checked", true);
                        $(`input[name='hosrSelfprice'][value!='${reg.hosrSelfprice}']`).prop("checked", false);

                        $(`input[name='hosrSex'][value='${reg.hosrSex}']`).prop("checked", true);
                        $(`input[name='hosrSex'][value!='${reg.hosrSex}']`).prop("checked", false);

                        $(`input[name='hosrLookdoctor'][value='${reg.hosrLookdoctor}']`).prop("checked", true);
                        $(`input[name='hosrLookdoctor'][value!='${reg.hosrLookdoctor}']`).prop("checked", false);

                        $("input[name='hosrWork']").val(reg.hosrWork);

                        // <select name="dKeishi" onchange="keshiChange()">
                        //         <option value="">---</option>
                        //         <option value="急诊科">急诊科</option>
                        //         <option value="骨科">骨科</option>
                        //         <option value="血液科">血液科</option>
                        // </select></td>
                        $(`select[name='dKeishi']>option[value='${reg.doctor.dKeshi}']`).prop("selected", true);
                        $(`select[name='dKeishi']>option[value!='${reg.doctor.dKeshi}']`).prop("selected", false);

                        // 刷新科室下的医生列表
                        keshiChange();

                        setTimeout(() => {
                            $(`select[name='dId']>option[value='${reg.doctor.dId}']`).prop("selected", true);
                            $(`select[name='dId']>option[value!='${reg.doctor.dId}']`).prop("selected", false);
                        }, 50);
                        // console.log("reg.doctor.dId is " + reg.doctor.dId);

                        $("input[name='hosrAge']").val(reg.hosrAge);
                        document.getElementById('hosrRemark').value = reg.hosrRemark;
                    }
                }
            })

        }

        function addRegistration() {
            // 不使用表单方式提交请求
            event.preventDefault();
            // 获取请求参数
            let hosrName = $("input[name='hosrName']").val();
            let hosrIdcar = $("input[name='hosrIdcar']").val();
            let hosrMedical = $("input[name='hosrMedical']").val();
            let hosrRegprice = $("input[name='hosrRegprice']").val();
            let hosrPhone = $("input[name='hosrPhone']").val();
            let hosrSelfprice = $("input[name='hosrSelfprice']:checked").val();
            let hosrSex = $("input[name='hosrSex']:checked").val();
            let hosrAge = $("input[name='hosrAge']").val();
            let hosrWork = $("input[name='hosrWork']").val();
            let dId = $("select[name='dId']>option:selected").val();
            let hosrRemark = document.getElementById('hosrRemark').value;

            let hosregister = {
                hosrName, hosrIdcar, hosrMedical, hosrRegprice, hosrPhone, hosrSelfprice, hosrSex, hosrAge,
                hosrWork, dId, hosrRemark
            }

            console.log(hosregister)

            $.ajax({
                type: "POST",
                url: "/registration/save",
                dataType: "json",
                data: hosregister,
                success: function (res) {
                    alert(res.msg);
                    if (res.code === 200) {
                        // 跳转至查询页
                        $(location).attr("href", "index.html");
                    }
                }
            });
        }

        // 当科室变化,同时查询当前科室下的所有医生
        function keshiChange() {
            let keshi = $("select[name='dKeishi']>option:selected").val();
            if (keshi == "") {
                $("select[name='dId']").html(null);
                return;
            }
            // 查询该科室下所有医生
            $.ajax({
                type: "GET",
                url: `/doctor/ks/${keshi}`,
                dataType: "json",
                success: function (res) {
                    if (res.code === 200) {
                        let dNameSelction = $("select[name='dId']");
                        let doctors = res.list;
                        // let str = "<option value=''>---</option>";
                        let str = "";
                        for (let i = 0; i < doctors.length; i++) {
                            let doctor = doctors[i];
                            // console.log("doctor.dId is " + doctor.dId);
                            str += `<option value='${doctor.dId}'>${doctor.dName}</option>`;
                        }
                        dNameSelction.html(str);
                    }
                }
            });
        }

        /**
         * 身份证验证
         */
        function idCarCheck() {
            let hosrIdcar = $("input[name='hosrIdcar']").val();
            console.log(hosrIdcar);
            if (hosrIdcar == undefined || hosrIdcar == '' || hosrIdcar.length == 0) {
                return;
            }
            let f = validateIdCard(hosrIdcar);
            if (!f) {
                alert("请输入正确的身份证号!")
                return;
            }
            // 根据身份证号码获取生日/性别等信息
            let sex = IdCard(hosrIdcar, 2);
            console.log(sex);
            let sexIn = $(`input[name='hosrSex'][value='${sex}']`);
            let sexOut = $(`input[name='hosrSex'][value!='${sex}']`);
            sexOut.prop("checked", false);
            sexIn.prop("checked", "checked");

            let age = IdCard(hosrIdcar, 3);
            console.log(age);
            $("input[name='hosrAge']").val(age);
        }

        // 根据身份证号获取出生日期/性别/年龄
        function IdCard(UUserCard, num) {
            if (num == 1) {
                //获取出生日期
                birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
                return birth;
            }
            if (num == 2) {
                //获取性别
                if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
                    //男
                    return 0;
                } else {
                    //女
                    return 1;
                }
            }
            if (num == 3) {
                //获取年龄
                var myDate = new Date();
                var month = myDate.getMonth() + 1;
                var day = myDate.getDate();
                var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
                if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
                    age++;
                }
                return age;
            }
        }
    </script>
</head>
<body>
<body>

<form action="index.html" id="regForm" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td><input type="text" name="hosrName" value=""/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">身份证号</td>
            <td><input type="text" onblur="idCarCheck()" name="hosrIdcar" value="512501197506045175"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">社保号</td>
            <td><input type="text" name="hosrMedical" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">挂号费(元)</td>
            <td><input type="text" name="hosrRegprice" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">联系电话</td>
            <td><input type="text" name="hosrPhone" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">是否自费</td>
            <td><input type="radio" name="hosrSelfprice" value="0" checked/>否&nbsp;&nbsp;&nbsp;<input type="radio"
                                                                                                      name="hosrSelfprice"
                                                                                                      value="1"/>是
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">性别</td>
            <td><input disabled type="radio" name="hosrSex" value="0" checked/>男&nbsp;&nbsp;&nbsp;<input type="radio"
                                                                                                         disabled
                                                                                                         name="hosrSex"
                                                                                                         value="1"/>女
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">年龄</td>
            <td><input disabled type="text" name="hosrAge" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">职业</td>
            <td><input type="text" name="hosrWork" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">初复诊</td>
            <td><input type="radio" name="hosrLookdoctor" value="0" checked/>初诊&nbsp;&nbsp;&nbsp;<input type="radio"
                                                                                                        name="hosrLookdoctor"
                                                                                                        value="1"/>复诊
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">所挂科室</td>
            <td><select name="dKeishi" onchange="keshiChange()">
                <option value="">---</option>
                <option value="急诊科">急诊科</option>
                <option value="骨科">骨科</option>
                <option value="血液科">血液科</option>
            </select></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">指定医生</td>
            <td>
                <select name="dId">
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><textarea id="hosrRemark"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button onclick="addRegistration()" class="btn btn-primary" type="button">保存</button>
                    &nbsp;&nbsp;<button
                        type="button" class="btn btn-success" name="backid" id="backid">返回列表
                </button>
                </center>
            </td>
        </tr>
    </table>
</form>

</body>
</html>